# WebSocket服务器Makefile
# 作者: 毕业论文项目

# 编译器设置
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
DEBUG_FLAGS = -g -DDEBUG
RELEASE_FLAGS = -O3 -DNDEBUG

# 包含目录
INCLUDE_DIRS = -Iinclude

# 库设置
LIBS = -lpthread -lssl -lcrypto

# 源文件
SERVER_SRCS = src/server/main.cpp \
              src/server/websocket_server.cpp \
              src/server/websocket_session.cpp \
              src/common/websocket_frame.cpp \
              src/common/utils.cpp

CLIENT_SRCS = src/client/main.cpp \
              src/client/websocket_client.cpp \
              src/common/websocket_frame.cpp \
              src/common/utils.cpp

TEST_SRCS = tests/test_server.cpp \
            src/server/websocket_server.cpp \
            src/server/websocket_session.cpp \
            src/client/websocket_client.cpp \
            src/common/websocket_frame.cpp \
            src/common/utils.cpp

# 目标文件
SERVER_OBJS = $(SERVER_SRCS:.cpp=.o)
CLIENT_OBJS = $(CLIENT_SRCS:.cpp=.o)
TEST_OBJS = $(TEST_SRCS:.cpp=.o)

# 可执行文件
SERVER_TARGET = bin/websocket_server
CLIENT_TARGET = bin/websocket_client
TEST_TARGET = bin/websocket_tests

# 默认目标
all: release

# 调试版本
debug: CXXFLAGS += $(DEBUG_FLAGS)
debug: $(SERVER_TARGET) $(CLIENT_TARGET) $(TEST_TARGET)

# 发布版本
release: CXXFLAGS += $(RELEASE_FLAGS)
release: $(SERVER_TARGET) $(CLIENT_TARGET) $(TEST_TARGET)

# 服务器程序
$(SERVER_TARGET): $(SERVER_OBJS)
	@mkdir -p bin
	$(CXX) $(CXXFLAGS) $(INCLUDE_DIRS) -o $@ $(SERVER_OBJS) $(LIBS)
	@echo "构建服务器程序: $@"

# 客户端程序
$(CLIENT_TARGET): $(CLIENT_OBJS)
	@mkdir -p bin
	$(CXX) $(CXXFLAGS) $(INCLUDE_DIRS) -o $@ $(CLIENT_OBJS) $(LIBS)
	@echo "构建客户端程序: $@"

# 测试程序
$(TEST_TARGET): $(TEST_OBJS)
	@mkdir -p bin
	$(CXX) $(CXXFLAGS) $(INCLUDE_DIRS) -o $@ $(TEST_OBJS) $(LIBS)
	@echo "构建测试程序: $@"

# 编译规则
%.o: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(INCLUDE_DIRS) -c $< -o $@

# 清理
clean:
	rm -rf bin
	find . -name "*.o" -delete
	@echo "清理完成"

# 安装
install: release
	@mkdir -p /opt/websocket-server
	cp $(SERVER_TARGET) /opt/websocket-server/
	cp $(CLIENT_TARGET) /opt/websocket-server/
	chmod +x /opt/websocket-server/websocket_server
	chmod +x /opt/websocket-server/websocket_client
	@echo "安装完成"

# 运行测试
test: debug
	./$(TEST_TARGET)

# 运行服务器
run-server: debug
	./$(SERVER_TARGET)

# 运行客户端
run-client: debug
	./$(CLIENT_TARGET)

# 依赖检查
check-deps:
	@echo "检查依赖..."
	@which $(CXX) >/dev/null || (echo "错误: $(CXX) 未安装" && exit 1)
	@pkg-config --exists openssl || (echo "错误: OpenSSL 未安装" && exit 1)
	@echo "所有依赖已安装"

# 帮助信息
help:
	@echo "WebSocket服务器构建系统"
	@echo ""
	@echo "目标:"
	@echo "  all        - 构建发布版本 (默认)"
	@echo "  debug      - 构建调试版本"
	@echo "  release    - 构建发布版本"
	@echo "  clean      - 清理构建文件"
	@echo "  install    - 安装到系统"
	@echo "  test       - 运行测试"
	@echo "  run-server - 运行服务器"
	@echo "  run-client - 运行客户端"
	@echo "  check-deps - 检查依赖"
	@echo "  help       - 显示此帮助信息"
	@echo ""
	@echo "示例:"
	@echo "  make debug           # 构建调试版本"
	@echo "  make test            # 运行测试"
	@echo "  make run-server      # 运行服务器"

.PHONY: all debug release clean install test run-server run-client check-deps help